
import json
import collections
import os, re


#============================================================================
f = open("e:\\w\\2.csv", 'r' ,encoding='utf8')
fo = open("e:\\w\\forms-verb-irregular(all).csv", 'w' ,encoding='utf8')
#fword = open("e:\\w\\verb_list.csv", 'w' ,encoding='utf8')


words = {}

for line in f.readlines():
    items=re.split("\|",line.strip())
    word                  =  items[0].strip() if len(items) >= 1 else None
    present_participle    =  items[1].strip() if len(items) >= 2  else ""
    past_participle       =  items[2].strip() if len(items) >= 3  else ""
    past_tense            =  items[3].strip() if len(items) >= 4  else ""
    third_person_singular =  items[4].strip() if len(items) >= 5  else ""

    #fword.write(word+"\n")
    if words.get(word) is None: words[word] = dict()

    form_from_book = present_participle
    key="present_participle"
    if len(form_from_book) :
        p_items =re.split(",",form_from_book)
        for pi in p_items:
            if re.search("^[a-z]+$", pi):
                pp=words[word].get(key)
                if pp is None: words[word][key] = dict()
                words[word][key][pi] = pi

    form_from_book = past_participle
    key="past_participle"
    if len(form_from_book) :
        p_items =re.split(",",form_from_book)
        for pi in p_items:
            if re.search("^[a-z]+$", pi):
                pp=words[word].get(key)
                if pp is None: words[word][key] = dict()
                words[word][key][pi] = pi

    form_from_book = past_tense
    key="past_tense"
    if len(form_from_book) :
        p_items =re.split(",",form_from_book)
        for pi in p_items:
            if re.search("^[a-z]+$", pi):
                pp=words[word].get(key)
                if pp is None: words[word][key] = dict()
                words[word][key][pi] = pi

    form_from_book = third_person_singular
    key="third_person_singular"
    if len(form_from_book) :
        p_items =re.split(",",form_from_book)
        for pi in p_items:
            if re.search("^[a-z]+$", pi):
                pp=words[word].get(key)
                if pp is None: words[word][key] = dict()
                words[word][key][pi] = pi

for word, info in words.items():
    present_participle = info.get("present_participle") or {}
    past_participle = info.get("past_participle") or {}
    past_tense = info.get("past_tense") or {}
    third_person_singular = info.get("third_person_singular") or {}
    
    if len(present_participle) < 1 and len(past_participle) < 1 and len(past_tense) < 1 and len(third_person_singular) < 1 : continue
    
    fo.write(word+"|"+"{}|{}|{}|{}\n".format(",".join(p for p,v in present_participle.items()),",".join(p for p,v in past_participle.items()),",".join(p for p,v in past_tense.items()),",".join(p for p,v in third_person_singular.items()) ))

fo.close()

#fword.close()

